package com.example.shop_front_api.controller;

import com.example.shop_front_api.entity.Product;
import com.example.shop_front_api.service.ProductService;
import com.example.shop_front_api.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("list")
    public JsonResult getProductList() {
        List<Product> productList = productService.getOnShelfProductsOrderBySales();
        return JsonResult.success("获取成功", productList);
    }

    @GetMapping("detail")
    public JsonResult getProductDetail(@RequestParam("id") Integer id) {
        Product product = productService.getProductById(id);
        if (product == null) {
            return JsonResult.failed("商品不存在");
        }
        return JsonResult.success("获取成功", product);
    }
}